package org.rsbot.script.methods;

import org.rsbot.bot.Bot;
import org.rsbot.client.Client;
import org.rsbot.client.input.Canvas;
import org.rsbot.script.randoms.LoginBot;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSTile;

public class Game extends MethodProvider
{
  public static final int INDEX_LOGIN_SCREEN = 3;
  public static final int INDEX_LOBBY_SCREEN = 7;
  public static final int[] INDEX_LOGGED_IN = { 10, 11 };
  public static final int INDEX_FIXED = 746;
  public static final int[] TAB_FUNCTION_KEYS = { 116, 0, 0, 0, 112, 113, 114, 115, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  public static final int TAB_ATTACK = 0;
  public static final int TAB_ACHIEVEMENTS = 1;
  public static final int TAB_STATS = 2;
  public static final int TAB_QUESTS = 3;
  public static final int TAB_INVENTORY = 4;
  public static final int TAB_EQUIPMENT = 5;
  public static final int TAB_PRAYER = 6;
  public static final int TAB_MAGIC = 7;
  public static final int TAB_SUMMONING = 8;
  public static final int TAB_FRIENDS = 9;
  public static final int TAB_IGNORE = 10;
  public static final int TAB_CLAN = 11;
  public static final int TAB_OPTIONS = 12;
  public static final int TAB_CONTROLS = 13;
  public static final int TAB_MUSIC = 14;
  public static final int TAB_NOTES = 15;
  public static final int TAB_LOGOUT = 16;
  public static final int CHAT_OPTION = 751;
  public static final int CHAT_OPTION_ALL = 2;
  public static final int CHAT_OPTION_GAME = 3;
  public static final int CHAT_OPTION_PUBLIC = 4;
  public static final int CHAT_OPTION_PRIVATE = 5;
  public static final int CHAT_OPTION_CLAN = 6;
  public static final int CHAT_OPTION_TRADE = 7;
  public static final int CHAT_OPTION_ASSIST = 8;
  public static final int INTERFACE_CHAT_BOX = 137;
  public static final int INTERFACE_GAME_SCREEN = 548;
  public static final int INTERFACE_LEVEL_UP = 740;
  public static final int INTERFACE_LOGOUT = 182;
  public static final int INTERFACE_LOGOUT_LOBBY = 1;
  public static final int INTERFACE_LOGOUT_COMPLETE = 6;
  public static final int INTERFACE_LOGOUT_BUTTON_FIXED = 181;
  public static final int INTERFACE_LOGOUT_BUTTON_RESIZED = 172;
  public static final int INTERFACE_WELCOME_SCREEN = 907;
  public static final int INTERFACE_WELCOME_SCREEN_CHILD = 150;
  public static final int INTERFACE_WELCOME_SCREEN_PLAY = 18;
  public static final int INTERFACE_HP_ORB = 748;
  public static final int INTERFACE_PRAYER_ORB = 749;
  public static final int[] INTERFACE_TALKS = { 211, 241, 251, 101, 242, 102, 161, 249, 243, 64, 65, 244, 255, 249, 230, 372, 421 };

  public static final int[] INTERFACE_OPTIONS = { 230, 228 };

  public static final String[] TAB_NAMES = { "Combat Styles", "Stats", "Quest List", "Achievements", "Inventory", "Worn Equipment", "Prayer List", "Magic Spellbook", "Objectives", "Friends List", "Ignore List", "Clan Chat", "Options", "Emotes", "Music Player", "Notes", "Exit" };

  Game(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  @Deprecated
  public boolean setTradeMode(CHAT_MODE paramCHAT_MODE)
  {
    if (paramCHAT_MODE.equals(CHAT_MODE.HIDE))
      throw new IllegalArgumentException("Bad mode: HIDE");
    mouseChatButton(7, false);
    return this.methods.menu.doAction(paramCHAT_MODE.toString());
  }

  @Deprecated
  public void showAllChatMessages()
  {
    mouseChatButton(2, true);
  }

  @Deprecated
  public void showGameChatMessages()
  {
    mouseChatButton(3, true);
  }

  @Deprecated
  public boolean setPrivateChat(CHAT_MODE paramCHAT_MODE)
  {
    if (paramCHAT_MODE.equals(CHAT_MODE.HIDE))
      throw new IllegalArgumentException("Bad mode: HIDE");
    mouseChatButton(5, false);
    return this.methods.menu.doAction(paramCHAT_MODE.toString());
  }

  @Deprecated
  public boolean setPublicChat(CHAT_MODE paramCHAT_MODE)
  {
    mouseChatButton(4, false);
    return this.methods.menu.doAction(paramCHAT_MODE.toString());
  }

  @Deprecated
  public boolean setAssistMode(CHAT_MODE paramCHAT_MODE)
  {
    if (paramCHAT_MODE.equals(CHAT_MODE.HIDE))
      throw new IllegalArgumentException("Bad mode: HIDE");
    mouseChatButton(8, false);
    return this.methods.menu.doAction(paramCHAT_MODE.toString());
  }

  @Deprecated
  public boolean setClanMode(CHAT_MODE paramCHAT_MODE)
  {
    if (paramCHAT_MODE.equals(CHAT_MODE.HIDE))
      throw new IllegalArgumentException("Bad mode: HIDE");
    mouseChatButton(6, false);
    return this.methods.menu.doAction(paramCHAT_MODE.toString());
  }

  public boolean setChatOption(int paramInt, CHAT_MODE paramCHAT_MODE)
  {
    mouseChatButton(paramInt, false);
    return this.methods.menu.doAction(paramCHAT_MODE.toString());
  }

  public String getLastMessage()
  {
    org.rsbot.script.wrappers.RSInterface localRSInterface = this.methods.interfaces.get(137);
    for (int i = 279; i >= 180; i--)
    {
      String str = localRSInterface.getComponent(i).getText();
      if ((!str.isEmpty()) && (str.contains("<")))
        return str;
    }
    return "";
  }

  public boolean openTab(int paramInt)
  {
    return openTab(paramInt, false);
  }

  public boolean openTab(int paramInt, boolean paramBoolean)
  {
    if (paramInt == getCurrentTab()) {
      return true;
    }
    if (paramBoolean) {
      if ((paramInt >= TAB_FUNCTION_KEYS.length) || (TAB_FUNCTION_KEYS[paramInt] == 0)) {
        return false;
      }
      this.methods.keyboard.pressKey((char)TAB_FUNCTION_KEYS[paramInt]);
      sleep(random(80, 200));
      this.methods.keyboard.releaseKey((char)TAB_FUNCTION_KEYS[paramInt]);
    } else {
      org.rsbot.client.RSInterface localRSInterface = this.methods.gui.getTab(paramInt);
      if (localRSInterface == null) {
        return false;
      }
      this.methods.interfaces.getComponent(localRSInterface.getID()).doClick();
    }

    sleep(random(400, 600));
    return paramInt == getCurrentTab();
  }

  public void closeTab()
  {
    int i = getCurrentTab();
    if ((isFixed()) || (i == 16)) {
      return;
    }
    org.rsbot.client.RSInterface localRSInterface = this.methods.gui.getTab(i);
    if (localRSInterface != null)
      this.methods.interfaces.getComponent(localRSInterface.getID()).doClick();
  }

  public boolean mouseChatButton(int paramInt, boolean paramBoolean)
  {
    RSComponent localRSComponent = this.methods.interfaces.get(751).getComponent(paramInt);

    if (!localRSComponent.isValid()) {
      return false;
    }
    return localRSComponent.doClick(paramBoolean);
  }

  public int getCurrentTab()
  {
    for (int i = 0; i < TAB_NAMES.length; i++)
    {
      org.rsbot.client.RSInterface localRSInterface = this.methods.gui.getTab(i);
      if (localRSInterface == null)
      {
        continue;
      }

      if (localRSInterface.getTextureID() != -1) {
        return i;
      }
    }
    return -1;
  }

  @Deprecated
  public int getEnergy()
  {
    return this.methods.walking.getEnergy();
  }

  public RSComponent getTalkInterface()
  {
    for (int k : INTERFACE_TALKS) {
      RSComponent localRSComponent = this.methods.interfaces.getComponent(k, 0);
      if (localRSComponent.isValid())
        return localRSComponent;
    }
    return null;
  }

  public boolean switchWorld(int paramInt)
  {
    if (isLoggedIn()) {
      logout(true);
    }
    if (getClientState() != 7) {
      return false;
    }
    RSComponent localRSComponent1 = this.methods.interfaces.getComponent(906, 196);
    if ((localRSComponent1.getBackgroundColor() != 2630) && 
      (localRSComponent1.doClick())) {
      for (int i = 0; localRSComponent1.getBackgroundColor() != 2630; i++) {
        if (i == 10) {
          return false;
        }
        sleep(random(100, 200));
      }

    }

    Object localObject = null;
    for (RSComponent localRSComponent2 : this.methods.interfaces.getComponent(910, 68).getComponents())
    {
      if (Integer.parseInt(localRSComponent2.getText()) == paramInt) {
        localObject = localRSComponent2;
        break;
      }
    }

    if (localObject == null) {
      return false;
    }
    for (int j = 0; !this.methods.interfaces.scrollTo(localObject, 59637845); )
    {
      if (j == 3) {
        return false;
      }
      sleep(random(200, 400));

      j++;
    }

    String str = this.methods.interfaces.getComponent(910, 70).getComponents()[localObject.getComponentIndex()].getText();

    if ((str.equals("0")) || (str.equals("OFFLINE")) || (str.equals("FULL")))
    {
      return false;
    }
    if (this.methods.interfaces.getComponent(910, 76).getComponents()[localObject.getComponentIndex()].doClick())
    {
      if (this.methods.interfaces.getComponent(906, 154).doClick()) {
        return true;
      }
    }
    return false;
  }

  public boolean isOnLogoutTab()
  {
    for (int i = 0; i < TAB_NAMES.length; i++) {
      org.rsbot.client.RSInterface localRSInterface = this.methods.gui.getTab(i);
      if (localRSInterface == null)
        continue;
      int j = localRSInterface.getTextureID();
      if ((j > -1) && (j < 2201))
        return false;
    }
    return true;
  }

  public boolean logout(boolean paramBoolean)
  {
    if (this.methods.bank.isOpen()) {
      this.methods.bank.close();
      sleep(random(200, 400));
    }
    if (this.methods.bank.isOpen())
      return false;
    int i;
    int j;
    if ((this.methods.client.isSpellSelected()) || (this.methods.inventory.isItemSelected()))
    {
      i = this.methods.game.getCurrentTab();
      j = random(1, 6);
      while (j == i) {
        j = random(1, 6);
      }
      this.methods.game.openTab(j);
      sleep(random(400, 800));
    }
    if ((this.methods.client.isSpellSelected()) || (this.methods.inventory.isItemSelected()))
    {
      return false;
    }
    if (!isOnLogoutTab()) {
      i = this.methods.client.getGUIRSInterfaceIndex();

      this.methods.interfaces.getComponent(i, isFixed() ? 181 : 172).doClick();

      j = 0;
      while ((!isOnLogoutTab()) && (j < 5)) {
        sleep(random(200, 400));
        j++;
      }
    }
    this.methods.interfaces.getComponent(182, paramBoolean ? 1 : 6).doClick();

    sleep(random(1500, 2000));
    return !isLoggedIn();
  }

  public boolean login()
  {
    return new LoginBot().activateCondition();
  }

  public boolean isFixed()
  {
    return this.methods.client.getGUIRSInterfaceIndex() != 746;
  }

  public boolean isLoggedIn()
  {
    Client localClient = this.methods.client;
    int i = localClient == null ? -1 : localClient.getLoginIndex();
    for (int m : INDEX_LOGGED_IN) {
      if (i == m) {
        return true;
      }
    }
    return false;
  }

  public boolean isLoginScreen()
  {
    return this.methods.client.getLoginIndex() == 3;
  }

  public boolean isWelcomeScreen()
  {
    return this.methods.interfaces.get(907).getComponent(150).getAbsoluteY() > 2;
  }

  public int getClientState()
  {
    return this.methods.client.getLoginIndex();
  }

  public int getPlane()
  {
    return this.methods.client.getPlane();
  }

  public int getBaseX()
  {
    return this.methods.client.getBaseX();
  }

  public int getBaseY()
  {
    return this.methods.client.getBaseY();
  }

  public RSTile getMapBase()
  {
    return new RSTile(this.methods.client.getBaseX(), this.methods.client.getBaseY());
  }

  public int getWidth()
  {
    return this.methods.bot.getCanvas().getWidth();
  }

  public int getHeight()
  {
    return this.methods.bot.getCanvas().getHeight();
  }

  public static enum CHAT_MODE
  {
    VIEW, ON, FRIENDS, OFF, HIDE;
  }
}